package edu.hut.bookshop.controller;


import com.alipay.api.AlipayApiException;
import com.github.pagehelper.PageInfo;
import edu.hut.bookshop.pojo.Order;
import edu.hut.bookshop.pojo.Pay;
import edu.hut.bookshop.pojo.User;
import edu.hut.bookshop.service.PayService;
import edu.hut.bookshop.util.ResultCode;
import edu.hut.bookshop.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;



    /**
     * 分页获取订单列表，如果都为空，则获取所有
     * @param
     * @param
     * @return
     */
   /* @GetMapping("/list")
    public ResultVO getOrderList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit) {
        List<Order> orders = orderHandleService.getAllOrdersByPage(page, limit);
        PageInfo pageInfo = new PageInfo(orders);
        return new ResultVO(ResultCode.SUCCESS,(int)pageInfo.getTotal(),orders);
    }*/
    //根据订单id查询支付订单
    @GetMapping("/findByorderId")
    public ResultVO findByorderId(Integer orderId){
      Pay pay =  payService.findByorderId(orderId);
        System.out.println("ddidw"+orderId);
        return new ResultVO(ResultCode.SUCCESS,pay);
    }

    @GetMapping("/findByuserId")
    public ResultVO findByuserId(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Pay> pays = payService.findByuserId(user.getUserId(), 2, 10);
        //System.out.println("yhidw"+user.getUserId());
        PageInfo pageInfo = new PageInfo(pays);
        return new ResultVO(ResultCode.SUCCESS,pays);
    }

    @GetMapping("/to_alipay")
    public String topay(Integer orderId)throws AlipayApiException {
        Pay pay = payService.findByorderId(orderId);
        return payService.Alipay(pay);
    }

    //支付成功支付宝调用方法：
    @RequestMapping(value = "ok")
    public void ok(){
        System.out.println("付款成功！");
    }
}
